/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBuscarMaestro.java
 *
 * Created on 19/10/2014, 04:06:39 PM
 */
package mx.com.ugto.caadi.asistencias.gui;

import java.util.List;
import mx.com.ugto.caadi.asistencias.bean.Maestro;
import mx.com.ugto.caadi.asistencias.dao.MaestroDAO;
import mx.com.ugto.caadi.asistencias.gui.model.ModeloTablaMaestros;

/**
 *
 * @author Rolando
 */
public class PanelBuscarMaestro extends javax.swing.JPanel {

    private ModeloTablaMaestros modeloTabla;
    
    /** Creates new form PanelBuscarMaestro */
    public PanelBuscarMaestro() {
        initComponents();
        modeloTabla = new ModeloTablaMaestros();
        tblMaestros.setModel(modeloTabla);
    }
    
    void cargarDatosTabla() {
        modeloTabla = new ModeloTablaMaestros();
        List<Maestro> lstMaestros;
        MaestroDAO dao = new MaestroDAO();
        lstMaestros = dao.findAll();
        for (Maestro maestro : lstMaestros) {
            modeloTabla.agregarAlumno(maestro);
        }
        tblMaestros.setModel(modeloTabla);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMenu = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        scrMaestros = new javax.swing.JScrollPane();
        tblMaestros = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        pnlMenu.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        pnlMenu.add(btnNuevo);

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        pnlMenu.add(btnEditar);

        add(pnlMenu, java.awt.BorderLayout.PAGE_START);

        tblMaestros.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrMaestros.setViewportView(tblMaestros);

        add(scrMaestros, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
// TODO add your handling code here:
    int filaSeleccionada = tblMaestros.getSelectedRow();
    Integer idAlumno = Integer.parseInt("" + modeloTabla.getValueAt(filaSeleccionada, 10));
    if (idAlumno >= 0) {
        MaestroDAO dao = new MaestroDAO();
        Maestro maestro = dao.find(idAlumno);
        Main.getInstance().editarMaestro(maestro);
    }
}//GEN-LAST:event_btnEditarActionPerformed

private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
// TODO add your handling code here:
    Main.getInstance().registrarMaestro();
}//GEN-LAST:event_btnNuevoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JPanel pnlMenu;
    private javax.swing.JScrollPane scrMaestros;
    private javax.swing.JTable tblMaestros;
    // End of variables declaration//GEN-END:variables
}
